<?php 

namespace Topxia\AdminBundle\Controller;

use Topxia\AdminBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
use Topxia\Common\Paginator;
use Topxia\Service\Common\ServiceException;


class TiXingController extends BaseController

{
	//题型管理的首页
	public function indexAction(Request $request)
	{  
		//获取题型的总数
		$total = $this->getTiXingService()->total();
		//分页
		$paginator = new Paginator($request,$total[0]["count('id')"], 20);
		//获取题型的信息
		$type = $this->getTiXingService()->get($paginator->getOffsetCount(), $paginator->getPerPageCount());

		return $this->render('TopxiaAdminBundle:Tixing:index.html.twig',array(

			'type' =>$type,
			'paginator' =>$paginator
	
			));

	}
	//题型添加
	public function addAction()
	{
		return $this->render('TopxiaAdminBundle:Tixing:add.html.twig');
	}

	//题型插入
	public function insertAction(Request $request)
	{
		$ptype = $request->get('ptype');
		$name= $request->get('name');
		$title = $request->get('title');
		$info = $this->getTiXingService()->insert($ptype,$name,$title);
		if($info)
		{	
			$this->setFlashMessage('success', '添加成功！');
			
		}else
		{
			$this->setFlashMessage('danger','添加失败');
		}

		return  $this->redirect($this->generateUrl('admin_tixing')); 

	}


	//题型删除
	public function deleteAction(Request $request)
	{
		$pid  = $request->get('id');
		$res = $this->getTiXingService()->delete($pid);
		if($res)
		{	
			$this->setFlashMessage('success', '删除成功！');
		}else
		{
			$this->setFlashMessage('danger','删除失败');

		}
			return  $this->redirect($this->generateUrl('admin_tixing')); 
		

	}




	public function getTiXingService()
	{
		return $this->getServiceKernel()->createService('TiXing.TiXingService');
	}

}


 ?>
